#include "ParserBase.h"

ParserBase::PARSED_COMMAND ParserBase::parseUserCommand(string userCommand){
	
	PARSED_COMMAND parsedCommand;
	bool result = tryParse(userCommand, parsedCommand);
	
	if (result == true) {
		return parsedCommand;
	}

	if (nextParser != NULL) {
		return nextParser->parseUserCommand(userCommand);
	}else {
		throw ("No parser exists to parse this command!");
	}
	
}

ParserBase::ParserBase(ParserBase* _nextParser) :nextParser(_nextParser) {
}

ptime ParserBase::parseDateTime(const string& timeStr) {
	stringstream timeStream(timeStr);
	ptime retTime;
	timeStream >> retTime;

	return retTime;
}

time_duration ParserBase::parseTime(const string& timeStr) {
	stringstream timeStream(timeStr);
	time_duration retTime;
	timeStream >> retTime;
	timeStream >> ws;

	bool invalidTime = retTime.is_not_a_date_time() || 
					   retTime >= hours(NUM_HOURS_IN_DAY) ||
					   !timeStream.eof();
	
	if (invalidTime) {
		return not_a_date_time;
	}
	
	return retTime;
}

int ParserBase::getTaskIDFromArgument(string argument) {
	int extractedID;
	stringstream strStream(argument);

	strStream >> extractedID;
	strStream >> ws;

	// ensure that we indeed have a task ID, and there are no other
	// trailing characters
	 assert(!strStream.fail() && strStream.eof());

	return extractedID;
}